# Send patch to Rails?
module ActiveResource
 class ActiveResourceError < StandardError
 end
 class RecordNotSaved < ActiveResourceError #:nodoc:
 end
 
 class Connection
   def get_raw(path, headers = {})
     request(:get, path, build_request_headers(headers)).body
   end
 end
 
 class Errors
   def to_xml(options={})
     options[:root] ||= "errors"
     options[:indent] ||= 2
     options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])

     options[:builder].instruct! unless options.delete(:skip_instruct)
     options[:builder].errors do |e|
       full_messages.each { |msg| e.error(msg) }
     end
   end
 end
  
 class Base
   
   def get_raw(method_name, options = {})
     connection.get_raw(custom_method_element_url(method_name, options), self.class.headers)
   end
   
   class << self
     def delete(id, options = {})
       connection.delete(element_path(id, options), self.headers)        
     end
   end
   
   def update_attributes(at)
     at.each do |key, value|
       if self.respond_to?(key)
         self.send(key + '=', value)
       end
     end
     self.save
   end
   
   def update_attributes!(at)
     update_attributes(at) || raise(RecordNotSaved)
   end
   
   def save!
    self.save || raise(RecordNotSaved)
   end
   
   def load(attributes)
     raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
     @prefix_options, attributes = split_options(attributes)
     attributes.each do |key, value|
       @attributes[key.to_s] =
         case value
           when Array
             # This is so we can return arrays as values but not break collections
             unless value.first.is_a?(Hash) or
                           (value.first.is_a?(Array) and value.first.first.is_a?(Hash))
               value.dup rescue value            
             else
               resource = find_or_create_resource_for_collection(key)
               value.map { |attrs| resource.new(attrs) }
             end
           when Hash
             resource = find_or_create_resource_for(key)
             resource.new(value)
           else
             value.dup rescue value
         end
     end
     self
   end
   
 end
end